//
// PowerUp - Sphinx adventure screen object handler / Dream teacher / Rat
//
// L. Dean Gibson II 3/2/01
//
//

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "adv_sphinx.h"
#include "adv_object_type.h"
#include "adventure_frame.h"
#include "ai_value.h"
#include "army.h"
#include "basic_dialog.h"
#include "creature_array.h"
#include "game_replace_keywords.h"
#include "hero.h"
#include "object_registration.h"
#include "player.h"
#include "replace_keywords.h"
#include "simple_dialog.h"

static t_object_registration<t_adv_sphinx> k_registration( k_adv_object_sphinx );


// -------------------------------------------------------------
// hero gains 500 exp per incremental visit to a sphinx
// -------------------------------------------------------------
t_adv_sphinx::t_adv_sphinx( std::string const& model_name )
         : t_single_use_object( model_name )
{
}

bool t_adv_sphinx::are_all_heroes_ineligible( t_army* army ) const
{
	t_creature_array& creatures = army->get_creatures();
	int                 i;
	t_hero*             hero;
	bool				any_heroes = false;
	t_player*           player = army->get_owner();
	bool				can_add_experience = false;

	for (i = 0; i < t_creature_array::k_size; i++)
	{
		hero = creatures[i].get_hero();
		if (hero == 0)
			continue;

		any_heroes = true;
	    
        if (!(hero->can_add_experience( player )))
            continue;
	
        can_add_experience = true;
    }

	if (any_heroes == false)
        return false;
    
    return (!can_add_experience);
}

// -------------------------------------------------------------
// hero gains 500 exp per incremental visit to a sphinx
// -------------------------------------------------------------
std::string t_adv_sphinx::add_icons( t_basic_dialog* dialog, std::string const& text, std::vector<t_hero*> const& heroes )
{
	int i;

	for (i = heroes.size() - 1; i >= 0; i--) {
		// add an icon of each hero that gained a benefit from this sphinx to the display
		// derive the exp award (already given by the time this is called) as one less than the hero's current * 500
		dialog->add_experience( 500 * heroes[i]->get_sphinx_count(), heroes[i] );
	}

	return replace_keywords( text, "%experience", "500" );
}

// -------------------------------------------------------------
// hero gains 500 exp per incremental visit to a sphinx
// -------------------------------------------------------------
void t_adv_sphinx::visit( t_hero* hero )
{
	hero->visit_sphinx();
	hero->add_experience( 500 * hero->get_sphinx_count() );
}

// -------------------------------------------------------------
// hero gains 500 exp per incremental visit to a sphinx
// -------------------------------------------------------------
float t_adv_sphinx::ai_value( t_adventure_ai const& ai, t_creature_array const& army, int move_cost ) const
{
	float value = 0.0f;
	for (int i = 0; i < t_creature_array::k_size; i++)
	{
		t_creature_stack const& stack = army[i];
		t_hero const* hero = stack.get_const_hero();
		if (hero && !hero->has_visited(*this)) 
		{
			float bonus = (float)(hero->get_sphinx_count() * 500);
			value += ai_value_of_xp( &army, bonus );
		}
	}
	return value;
}
